package com.JUtils.excel;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class ExcleOutPutHelper {

    public static void output(HttpServletResponse response, String fileName, HSSFWorkbook wb){
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("Content-disposition", "attachment; filename="+fileName);// 设定输出文件头
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");// 定义输出类型
            ServletOutputStream out = null;
            out = response.getOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(out);
            wb.write(bos);
            bos.flush();
            bos.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
